$().ready(function() {
	validateRule();
	$("#initImageFile").fileupload({
		dataType: 'json',
		done: function (e, data) {
			$("#image").val(data.result.url); 
			$("#img").attr("src",data.result.ip+data.result.url);
		}
	});
	$("select").select2();
	
	//类型改变，对应下拉框商品也进行改变（1：商品   2：商品分类）
	$('input[type=radio][name=type]').change(function() {
		debugger;
        if (this.value == '1') {
        	jQuery.ajax({
                url: "/shop/ad/selectUrl?type="+this.value,
                type: "GET",
                dataType: "json",
                success: function(msg) {
                    $("#url option").remove();
                    $("#url").append("<option value=''>请选择</option>");
                    $(msg.listPreProduct).each(function(index,element){
                    	$("#url").append("<option value='"+element.id+"'>"+element.name+"</option>");
                      });
                },
            });
        }else if(this.value == '2'){
        	jQuery.ajax({
                url: "/shop/ad/selectUrl?type="+this.value,
                type: "GET",
                dataType: "json",
                success: function(msg) {
                    $("#url option").remove();
                    $("#url").append("<option value=''>请选择</option>");
                    $(msg.listPreProduct).each(function(index,element){
                    	debugger;
                    	var level="";
                    	if(element.level==1){
                    		level="一级";
                    	}
                    	if(element.level==2){
                    		level="二级";
                    	}
                    	$("#url").append("<option value='"+element.id+"'>"+element.name+'&nbsp;&nbsp;&nbsp;&nbsp;'+level+"</option>");
                      });
                },
            });
        }
    });
});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/shop/ad/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入名字"
			}
		}
	})
}